package com.neudu.yyzx_service.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neudu.yyzx_service.common.JSONReturn;
import com.neudu.yyzx_service.pojo.Backdown;
import com.neudu.yyzx_service.pojo.Customer;
import com.neudu.yyzx_service.service.BackdownService;
import com.neudu.yyzx_service.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
public class BackdownController {
    @Autowired
    private BackdownService backdownService;
    
    @Autowired
    private CustomerService customerService;
    
    @Autowired
    private JSONReturn jsonReturn;
    
    /**
     * 获取所有退住记录（分页）
     * @param pageInfo 分页信息
     * @return 退住记录列表
     */
    @RequestMapping("get_all_backdowns")
    public String getAllBackdowns(@RequestBody PageInfo pageInfo) {
        try {
            PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
            List<Backdown> list = backdownService.getAllBackdownsWithCustomerName();

            PageInfo<Backdown> pageInfoReturn = new PageInfo<>(list);
            return jsonReturn.returnSuccess(pageInfoReturn);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
    
    /**
     * 根据客户姓名查询退住记录
     * @param backdown 包含客户姓名的退住记录
     * @return 退住记录列表
     */
    @RequestMapping("get_backdowns_by_name")
    public String getBackdownsByName(@RequestBody Backdown backdown) {
        try {
            List<Backdown> list = backdownService.getBackdownsByCustomerName(backdown.getCustomerName());
            return jsonReturn.returnSuccess(list);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
    
    /**
     * 添加退住记录
     * @param backdown 退住记录信息
     * @return 添加结果
     */
    @RequestMapping("add_one_backdown")
    public String addOneBackdown(@RequestBody Backdown backdown) {
        try {
            // 设置默认值
            backdown.setIsDeleted(1);
            if (backdown.getAuditstatus() == null) {
                backdown.setAuditstatus(1); // 默认审批状态为"同意"
            }
            backdown.setAudittime(new Date()); // 设置审批时间为当前时间
            
            backdownService.save(backdown);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
    
    /**
     * 根据ID获取退住记录信息
     * @param backdownId 退住记录ID
     * @return 退住记录信息
     */
    @RequestMapping("get_backdown_by_id")
    public String getBackdownById(@RequestParam("backdownId") Integer backdownId) {
        try {
            // 使用自定义方法获取带有客户姓名的退住记录
            Backdown backdown = backdownService.getBackdownWithCustomerNameById(backdownId);
            if(backdown == null) {
                // 如果没有找到对应的记录，使用原方法获取
                LambdaQueryWrapper<Backdown> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(Backdown::getId, backdownId);
                backdown = backdownService.getOne(queryWrapper);
                
                // 如果找到记录且有客户ID，尝试获取客户姓名
                if(backdown != null && backdown.getCustomerId() != null) {
                    Customer customer = customerService.getById(backdown.getCustomerId());
                    if(customer != null) {
                        backdown.setCustomerName(customer.getCustomerName());
                    }
                }
            }
            return jsonReturn.returnSuccess(backdown);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
    
    /**
     * 根据ID修改退住记录
     * @param backdown 退住记录信息
     * @return 修改结果
     */
    @RequestMapping("modify_backdown_by_id")
    public String modifyBackdownById(@RequestBody Backdown backdown) {
        try {
            backdownService.updateById(backdown);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
    
    /**
     * 根据ID删除退住记录
     * @param backdownId 退住记录ID
     * @return 删除结果
     */
    @RequestMapping("remove_backdown_by_id")
    public String removeBackdownById(@RequestParam("backdownId") Integer backdownId) {
        try {
            backdownService.removeById(backdownId);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
} 